package logic

import (
	"context"
	"fmt"
	"math/rand"
	"shop-hub/cache"
	"time"

	"shop-hub/shoprpc/internal/svc"
	"shop-hub/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 短信
func (l *SendSmsLogic) SendSms(in *shoprpc.SendSmsRequest) (*shoprpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	i, _ := cache.Rdb.Get(l.ctx, "sendSms").Int64()
	if i > 1 {
		return nil, fmt.Errorf("当前已发送一次短信")
	}

	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Account, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if sms.Code != 2 {
	//	return nil, fmt.Errorf(sms.Msg)
	//}

	cache.Rdb.Set(l.ctx, "sendSms"+in.Account, code, time.Minute*3)
	return &shoprpc.SendSmsResponse{}, nil
}
